using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlTypes;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace FutureOpen.Core.func.sql
{
    public class PicProvider
    {
        public static readonly int PIC_TYPE_ICON_USR = 1001;
        public static readonly int PIC_TYPE_ICON_GRP = 2001;
        public static readonly int PIC_TYPE_ICON_PIC = 3001;


        public static int AddPic(int type, byte[] blob, int usr)
        {
            return AddPic(string.Empty, type, blob, usr); 
        }

        
        public static int AddPic(MySqlTransaction tran, int type, byte[] blob, int usr)
        {
            return AddPic(tran, string.Empty, type, blob, usr);
        }
        

        public static readonly string SQL_ADD_PIC = "insert into pic(pic_name,pic_type,pic_content,pic_usr) values(?name,?type,?content,?usr);select last_insert_id();";
        public static int AddPic(string name, int type, byte[] blob, int usr)
        {
            MySqlParameter param1 = new MySqlParameter("?name", name);
            MySqlParameter param2 = new MySqlParameter("?type", type);
            MySqlParameter param3 = new MySqlParameter("?content", MySqlDbType.VarBinary, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
            MySqlParameter param4 = new MySqlParameter("?usr", usr);
            MySqlParameter[] pms = new MySqlParameter[] { param1, param2, param3, param4 };

            return Convert.ToInt32(MySqlHelper.ExecuteScalar(MySqlHelper.CONNSTR(), SQL_ADD_PIC, pms));
          
        }
        
        public static int AddPic(MySqlTransaction tran, string name, int type, byte[] blob, int usr)
        {
            MySqlParameter param1 = new MySqlParameter("?name", name);
            MySqlParameter param2 = new MySqlParameter("?type", type);
            MySqlParameter param3 = new MySqlParameter("?content", MySqlDbType.VarBinary, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
            MySqlParameter param4 = new MySqlParameter("?usr", usr);
            MySqlParameter[] pms = new MySqlParameter[] { param1, param2, param3, param4 };

            return Convert.ToInt32(MySqlHelper.ExecuteScalar(tran,SQL_ADD_PIC, pms));

        }



        public static readonly string SQL_UPDATE_PIC = "update pic set pic_name=?name,pic_content=?content where pic_id=?id";
        public static void UpdatePic(int id, string name, byte[] blob)
        {
            MySqlParameter param1 = new MySqlParameter("?name", name);
            MySqlParameter param2 = new MySqlParameter("?id", id);
            MySqlParameter param3 = new MySqlParameter("?content", MySqlDbType.VarBinary, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
            MySqlParameter[] pms = new MySqlParameter[] { param1, param2, param3};

            MySqlHelper.ExecuteNonQuery(MySqlHelper.CONNSTR(), SQL_UPDATE_PIC, pms);
        }

        public static readonly string SQL_DEL_PIC = "delete from pic where pic_id=0";
        public static void DelPic(int picid)
        {
            string sql = string.Format(SQL_DEL_PIC, picid);
            MySqlHelper.ExecuteNonQuery(MySqlHelper.CONNSTR(), sql, null);
        }

        public static readonly string SQL_GET_PIC = "select * from pic where pic_id={0}";
        public static DataRow GetPic(int pid)
        {
            string sql = string.Format(SQL_GET_PIC, pid);
            return MySqlHelper.ExecuteDatarow(MySqlHelper.CONNSTR(), sql, null);
        }        
    }
}
